package com.qm.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.qm.dao.AdminCourseDAO;
import com.qm.dao.AdminStudentDAO;
import com.qm.model.Classes;
import com.qm.model.Course;
import com.qm.model.ImportExcelReprotModel;
import com.qm.model.Topic;
import com.qm.model.TopicTest;
import com.qm.model.Users;
import com.qm.util.*;

@Service
public class AdminCourseService {
	@Autowired
	@Qualifier("adminCourseDao")
	private AdminCourseDAO acousedao;


	public AdminCourseDAO getAcousedao() {
		return acousedao;
	}
	public void setAcousedao(AdminCourseDAO acousedao) {
		this.acousedao = acousedao;
	}
	/**
	 * 班级信息分页显示
	 * @param page
	 * @param count
	 * @return
	 */
	public ArrayList<Classes> classesInformationPaging( int page,int count){
		ArrayList<Classes> classInfo=acousedao.classesInformationPaging(page, count);
		return classInfo;
		
	}
	/**
	 * 班级信息条数
	 * @return
	 */
	public int classesInformationCount(){
		int classCount=acousedao.classesInformationCount();
		return  classCount;
	}
	/**
	 * 增加班级
	 * @param c
	 * @return
	 */
	public int insertClass(Classes c){
		int result =acousedao.insertClass(c);
		return result;
	}
	/**
	 * 通过班级Id删除该班级
	 * @param cid
	 * @return
	 */
	public int deleteClassByCid(int cid){
		int result =acousedao.deleteClassByCid(cid);
		return result;
	}
	/**
	 * 课程名称和代码，为下拉菜单做准备
	 * @return
	 */
	public ArrayList<Course> selectCourseName(){
		ArrayList<Course> courseName=acousedao.selectCourseName();
		return courseName;
	}
	/**
	 * 根据课程代码来选择课程名称
	 * @param cocourseCode
	 * @return
	 */
	public  Course selectCourseBycocourseCode(int cocourseCode ){
		Course result=acousedao.selectCourseBycocourseCode(cocourseCode);
		return result;
	}
	/**
	 * 根据课程代码来修改课程信息
	 * @param c
	 * @return
	 */
	public int updateCourseBYcocourseCode(Course c){
		int result =acousedao.updateCourseBYcocourseCode(c);
		return result;
	}
	/**
	 * 新增课程信息
	 * @param c
	 * @return
	 */
	public int insertCourse(Course c){
		int result =acousedao.insertCourse(c);
		return result;
	}
	/**
	 * 删除课程信息
	 * @param cocourseCode
	 * @return
	 */
	public int deleteCourseByCocourseCode(int cocourseCode){
		int result=acousedao.deleteCourseByCocourseCode(cocourseCode);
		return result;
	}
	/**
	 * 课程信息分页显示
	 * @param page
	 * @param count
	 * @return
	 */
	public ArrayList<Course> selectCoursePaging( int page,int count){
		ArrayList<Course> result=acousedao.selectCoursePaging(page, count);
		return  result;
		
	}
	/**
	 * 统计课程数，为分页做准备
	 * @return
	 */
	public int courseCount(){
		int result=acousedao.courseCount();
		return result;
	}
	/**
	 * 按照课程名称来寻找班级
	 * @return
	 */
	public ArrayList<Classes> selectClasses(int cocourseCode){
		ArrayList<Classes> classes=acousedao.selectClasses(cocourseCode);
		return classes;
	}
    /**
     * 根据课程名称来显示班级，分页
     * @param cocourseCode
     * @param page
     * @param count
     * @return
     */
	public ArrayList<Classes> selectClassesByCourse(int cocourseCode,int page,int count){
	ArrayList<Classes> result=acousedao.selectClassesByCourse(cocourseCode, page, count);
	return result;
	}
	/**
	 * 根据课程名称来查询班级的数目
	 * @param cocourseCode
	 * @return
	 */
	public int classesCountByCourse(int cocourseCode ){
		int result=acousedao.classesCountByCourse(cocourseCode);
		return result;
	}
	/**
	 * 修改班级信息
	 * @param c
	 * @return
	 */
	public int updateClassByCid(Classes c){
	 int result=acousedao.updateClassByCid(c);
	 return result;
	}
	/**
	 * 通过班级班级编号查询班级信息
	 * @param cid
	 * @return
	 */
	public Classes selectClassByCid(int cid){
		Classes result=acousedao.selectClassByCid(cid);
		return result;
	}
	/**
	 * 导入题目信息
	 * @param excel
	 * @return
	 */
	public List<ImportExcelReprotModel<Topic>> importTopics(MultipartFile excel,int tcourseCode){
		List<ImportExcelReprotModel<Topic>> error =new ArrayList<>();
		//ImportExcelReprotModel<Users> reprtoModel=new ImportExcelReprotModel<>();
		try {
			String fileName=excel.getOriginalFilename();
			if(fileName.endsWith(".xls")){
				//2003
			}else{
				ArrayList<ImportExcelReprotModel<Topic>> topics=TopicExcelImport.parseTopic(excel);
				//int count=0;
			
				for(int i=0;i<topics.size();i++){
					
					if(topics.get(i).getErrorMsg()==null||topics.get(i).getErrorMsg().isEmpty()){
						
						topics.get(i).getData().setTtopic(StringEscapeUtils.escapeHtml(topics.get(i).getData().getTtopic()));
						topics.get(i).getData().setTanswerAnalysis(StringEscapeUtils.escapeHtml(topics.get(i).getData().getTanswerAnalysis()));
						topics.get(i).getData().setTcorrect(StringEscapeUtils.escapeHtml(topics.get(i).getData().getTcorrect()));
						topics.get(i).getData().setTmistake1(StringEscapeUtils.escapeHtml(topics.get(i).getData().getTmistake1()));
						topics.get(i).getData().setTmistake2(StringEscapeUtils.escapeHtml(topics.get(i).getData().getTmistake2()));
						topics.get(i).getData().setTmistake3(StringEscapeUtils.escapeHtml(topics.get(i).getData().getTmistake3()));
						topics.get(i).getData().setTcourseCode(tcourseCode);
						acousedao.importTopics(topics.get(i).data);
			
					}
					else{
						//reprtoModel=users.get(i);
						error.add(topics.get(i));
					}	   
				}
			}
				
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		return error;
	}
	/**
	 * 查询某课程的题目信息
	 * @param cocourseCode
	 * @param page
	 * @param count
	 * @return
	 */
	public ArrayList<TopicTest> selectTopicByCourse(int cocourseCode,int page,int count )
	{
		ArrayList<Topic> tpList=acousedao.selectTopicByCourse(cocourseCode, page, count);
		ArrayList<TopicTest> tsList=new ArrayList<>();
		for (Topic topic : tpList) {
			tsList.add(new TopicTest(topic));
		}
		return tsList;
	}
	/**
	 * 查询某课程的题目总数
	 * @param cocourseCode
	 * @return
	 */
	public int selectTopicCountByCourse(int cocourseCode){
		int result =acousedao.selectTopicCountByCourse(cocourseCode);
		return result;
	}
	
}
